from django.contrib.auth.mixins import AccessMixin, LoginRequiredMixin
from django.http import HttpRequest,JsonResponse


# # 第一种重写方式
# class LoginRequiredJSONMixin(AccessMixin):
#     """Verify that the current user is authenticated."""

#     def dispatch(self, request:HttpRequest, *args, **kwargs):
#         if not request.user.is_authenticated:
#             return JsonResponse({"code":400, "errmsg":"用户未登录"})
#         return super().dispatch(request, *args, **kwargs)
    

# 第二种重写方式
class LoginRequiredJSONMixin(LoginRequiredMixin):
    """Verify that the current user is authenticated."""

    def handle_no_permission(self):
        return JsonResponse({'code': 400, 'errmsg': '用户未登录'})